package dn.easy.common.utils.bottompopmenu;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.List;
import dn.easy.common.R;
import dn.easy.common.commonwidget.timeSelector.ScreenUtil;

/**
 * Class name 选择发票类型的底部弹出对话框
 * Created by  2018/5/23 0023 10:41.
 * Author by yangjing
 */
public class BottomPopMenu{

    private Context mContext;
    private Dialog seletorDialog;
    private ListView lv_content;
    private TextView tv_cancle;
    private TextView tv_select;
    private List<InvoiceTypeEntity> lists;
    private TextAdatper mTextAdatper ;
    private String choseTex;

    private OnDialogButtonClickListener buttonClickListner;

    public BottomPopMenu(Context context, List<InvoiceTypeEntity> lists) {
        this.mContext = context;
        this.lists = lists;
        initDialog();
        initView();
    }

    private void initView() {
        lv_content = (ListView) seletorDialog.findViewById(R.id.lv_content);
        tv_cancle = (TextView) seletorDialog.findViewById(R.id.tv_cancle);
        tv_select = (TextView) seletorDialog.findViewById(R.id.tv_select);
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                seletorDialog.dismiss();
            }
        });
        mTextAdatper = new TextAdatper(mContext, lists);
        lv_content.setAdapter(mTextAdatper);
    }

    private void initDialog() {
        if (seletorDialog == null) {
            seletorDialog = new Dialog(mContext, R.style.time_dialog);
            seletorDialog.setCancelable(true);
            seletorDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            seletorDialog.setContentView(R.layout.dialog_bottom_pop);
            Window window = seletorDialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            WindowManager.LayoutParams lp = window.getAttributes();
            int width = ScreenUtil.getInstance(mContext).getScreenWidth();
            lp.width = width;
            window.setAttributes(lp);
        }
    }

    public void show(final TextView textView){
        seletorDialog.show();
        tv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText(choseTex);
                seletorDialog.dismiss();
            }
        });

        lv_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                choseTex = lists.get(position).getName();
                textView.setText(choseTex);
                seletorDialog.dismiss();
                buttonClickListner.ListOnItemOnclick(position);
            }
        });
    }

    public interface OnDialogButtonClickListener{
        public void ListOnItemOnclick(int position);
    }

    public void setOnButtonClickListener(OnDialogButtonClickListener listener) {
        this.buttonClickListner = listener;
    }

}
